﻿#include "MainWindow.h"
#include "ui_MainWindow.h"

#include <QDebug>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::slot_OpenExcel(void)
{
    QString excelFileName = QFileDialog::getOpenFileName(this,"Select Excel","","表格(*.xls *.xlsx)");

    if(excelFileName != nullptr)
    {
        m_ExcelEngine = new ExcelEngine(excelFileName);
    }
}


void MainWindow::slot_ReadExcel()
{

    bool opened = m_ExcelEngine->open();

    if(opened)
    {

        m_ExcelEngine->getWorkSheetRange("格式示例",10,1,14,20);

        m_ExcelEngine->close();
    }

}

void MainWindow::slot_OpenWord(void)
{
    QString wordFileName = QFileDialog::getOpenFileName(this,"Select Word","defaultWordName.docx","文档(*.doc *.docx)");
    qDebug()<<"wordFileName:"<<wordFileName;
    if(!wordFileName.isEmpty())
    {
        m_WordEngine = new WordEngine(wordFileName);
    }else
    {
        return;
    }

    m_WordEngine->open();
    // m_WordEngine->insertText("[插入文字]");
    m_WordEngine->close();
}

// 解析表格内容
QStringList MainWindow::getTable(QString workSheet_Name)
{
    QMap<int,int> tableHeader;
    int rows,columns;
    QStringList tableString;

    // 用于判断识别table是哪个类型
    QString cellTemp,cellTemp2,cellTemp3;

    rows = m_ExcelEngine->getWorkSheetRows(workSheet_Name);
    columns = m_ExcelEngine->getWorkSheetColumns(workSheet_Name);

    // 解析出worksheet中的table
    for(int i = 1;i <= columns; i++)
    {
        cellTemp = m_ExcelEngine->getCellString(workSheet_Name,1,i);
        cellTemp2 = m_ExcelEngine->getCellString(workSheet_Name,1,i+1);
        cellTemp3 = m_ExcelEngine->getCellString(workSheet_Name,2,i+1);

        if(cellTemp != "")
        {
            if(cellTemp2 != "")
            {
                // 记录：第3行第i列，第三种样式表
                tableHeader.insert(i,3);
                i = i+5;
            }else if(cellTemp3 != "")
            {
                tableHeader.insert(i,1);
                i = i+3;
            }else
            {
                tableHeader.insert(i,2);
                i = i+4;
            }
        }
    }

    QString tableString_Temp = "";
    auto iter = tableHeader.begin();

    while(iter != tableHeader.end())
    {
        // 判断table的类别
        if(iter.value() == 1)
        {
            tableString_Temp = table2String(workSheet_Name,1,iter.key(),rows,3);
            tableString.append(tableString_Temp);

        }else if(iter.value() == 2)
        {
            tableString_Temp = table2String(workSheet_Name,1,iter.key(),rows,4);
            tableString.append(tableString_Temp);

        }else if(iter.value() == 3)
        {
            tableString_Temp = table2String(workSheet_Name,1,iter.key(),rows,5);
            tableString.append(tableString_Temp);
        }

        iter++;
    }

    return tableString;
}

// 根据table在表格中的左上角坐标和总行数总列数解析表格
QString MainWindow::table2String(QString workSheet_Name,int start_Row,int start_Column,int rows,int columns)
{
    QString tableString = "";
    QString colSeparator = "\\t";
    QString rowSeparator = "\\n";

    for(int i = start_Row;i<=rows;i++)
    {

        QString rowString = "";

        // 列数减去一，为了避免遍历空列
        for(int j = start_Column;j<= start_Column + columns-1;j++)
        {
            QString cellString = m_ExcelEngine->getCellString(workSheet_Name,i,j);
            rowString += cellString;

            if(cellString != "")
            {
                rowString += colSeparator;
            }
        }

        if(rowString != "")
        {
            // 去掉每一行最后一个colSeparator,要减去去掉colSeparator的长度
            rowString = rowString.left(rowString.length() - colSeparator.length());
            rowString += rowSeparator;
        }

        tableString += rowString;
    }

    // 去掉每一个表格最后一个rowSeparator，要减去rowSeparator的长度
    tableString = tableString.left(tableString.length() - rowSeparator.length());
    tableString.trimmed();

    return tableString;
}
